//
//  EnterNewPWViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-8.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "EnterNewPWViewController.h"
#import "LoginViewController.h"

#define kLEFT   10001
#define kRIGHT  10002

@implementation EnterNewPWViewController

- (id)initWithPassType:(PassType)passType
{
    self = [super initWithNibClass:[EnterNewPWViewController class]];
    if (self) {
        _passType = passType;
        _dataSource = [[EnterNewPWDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    
    NSString *title = _passType == Forgot ? @"找回密码" : @"修改密码";
    [self createNavigation:title];
    
    if (_passType == Forgot) 
        [self createRightButton:@"登录"];
    
    [_contentView drawBorder];
    [self.view addTarget:self action:@selector(viewTapped:)];
    
    self.userNameLabel.text = [AppSetting userName];
    self.phoneNumLabel.text = [AppSetting tel];
}

- (void)rightButtonClick:(id)sender
{
    [self pushOrPopViewController:[LoginViewController class]];
}

- (IBAction)buttonPress:(id)sender
{
    _userName = [AppSetting userName];
    _tel = [AppSetting tel];
    _password = _passwordTF.text;
    
    if (![self checkForm]) return;
    [_dataSource updatePassword];
}

-(void)viewTapped:(UITapGestureRecognizer*)tapGr
{
    [_passwordTF resignFirstResponder];
    [_confirmPassTF resignFirstResponder];
}

- (BOOL)checkForm
{
    if (_passwordTF.text.length <= 0) {
        [AppDelegate showMessage:@"密码不能为空"];
        return NO;
    }
    
    if (_confirmPassTF.text.length <= 0) {
        [AppDelegate showMessage:@"确认密码不能为空"];
        return NO;
    }
    
    if (![_passwordTF.text isEqualToString:_confirmPassTF.text]) {
        [AppDelegate showMessage:@"两次密码不一致"];
        return NO;
    }
    
    return YES;
}

@end

//==========================================================================================
#pragma ===================数据源类实现=======================================================
//==========================================================================================

@implementation EnterNewPWDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)updatePassword
{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:API_RESET_PASS,_viewController.tel,_viewController.password];
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
    
    NSDictionary *data = [json objectForKey:@"data"];
    [self processRequest:data];
}

- (void)processRequest:(NSDictionary *)data
{
    int state = [[data objectForKey:@"state"] integerValue];
    if (state == 1) {
        [SVProgressHUD dismissWithSuccess:@"密码修改成功"];
        [AppSetting loginOut];
        
        [_viewController performSelector:@selector(pushOrPopViewController:) withObject:[LoginViewController class] afterDelay:1.5];
    }
    if (state == 2) {
        [SVProgressHUD dismissWithError:@"密码修改不成功"];
    }
}

@end

